package com.luck.easyfoodbatch.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.glide.GlideCircleTransform;
import com.luck.easyfoodbatch.mvp.model.ClassModel;
import com.luck.easyfoodbatch.util.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * author：luck
 * project：LoveCare
 * package：com.tongyu.luck.lovecare.adapter
 * email：893855882@qq.com
 * data：16/8/12
 */
public class ImportAdapter extends RecyclerView.Adapter<ImportAdapter.ViewHolder> {
    private Context mContext;
    private List<ClassModel.DataBean.ImportBean> list = new ArrayList<>();
    private List<Integer> ivList;
    private int[] images = {R.mipmap.home_jiulei, R.mipmap.home_shuilei,
            R.mipmap.home_yinliao, R.mipmap.home_rupin, R.mipmap.home_lingshi,
            R.mipmap.home_liangyou,
            R.mipmap.home_chongyin, R.mipmap.home_muying};

    private void init(){
        ivList=new ArrayList<>();
        ivList.add(R.mipmap.home_jiulei);
        ivList.add(R.mipmap.home_shuilei);
        ivList.add(R.mipmap.home_yinliao);
        ivList.add(R.mipmap.home_rupin);
        ivList.add(R.mipmap.home_lingshi);
        ivList.add(R.mipmap.home_liangyou);
        ivList.add(R.mipmap.home_chongyin);
        ivList.add(R.mipmap.home_muying);

    }
    protected OnItemClickListener mItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }

    public ImportAdapter(Context context) {
        this.mContext = context;
        init();
    }

    public void setList(List<ClassModel.DataBean.ImportBean> list) {
         this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.class_item, null);
        return new ViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Integer integer = ivList.get(position);
        ClassModel.DataBean.ImportBean bean = list.get(position);
        String img = bean.getImg_home();
//        if (!Tools.isNull(img)) {
//            Glide.with(mContext)
//                    .load(HttpUtil.IMG_BASE_URL + img)
//                    .centerCrop()
//                    .placeholder(R.mipmap.home_jiushui_1x)
//                    .diskCacheStrategy(DiskCacheStrategy.ALL)
//                    .transform(new GlideCircleTransform(mContext))
//                    .into(holder.iv_icon);
//        } else {
//            holder.iv_icon.setImageResource(R.mipmap.niunai_1x);
//        }
        if (!Tools.isNull(integer)) {
            Glide.with(mContext).load(ivList.get(position)).centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .transform(new GlideCircleTransform(mContext))
                    .into(holder.iv_icon);
        } else {
            holder.iv_icon.setImageResource(R.mipmap.home_jiulei);
        }
        holder.tv_title.setText(bean.getName());
//        holder.tv_title.setText("酒类");
        //itemView 的点击事件
        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClickListener.onItemClick(holder.getAdapterPosition(), v);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView iv_icon;
        TextView tv_title;

        public ViewHolder(View view) {
            super(view);
            iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
            tv_title = (TextView) view.findViewById(R.id.tv_title);
        }
    }


}
